﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;

namespace Chalange2
{
    class Program
    {
        public static IEnumerable<Vendor> GetVendors() 
        {
            VendorEntities entities = new VendorEntities();
            IEnumerable<Vendor> vendors =
                from v in entities.Vendors
                select v;
            return vendors;

        }

        public static void ExtractToPdf(string fileName, string Header,IEnumerable< Entery> enteries) 
        {
            using (Document doc = new Document())
            {
                PdfWriter.GetInstance(doc, new FileStream(fileName, FileMode.Create));
                doc.Open();
                doc.SetPageSize(PageSize.A4.Rotate());

                PdfPTable table = new PdfPTable(5);
                PdfPCell cell = new PdfPCell(new Phrase(Header));
                cell.Colspan = 5;
                //cell.BackgroundColor
                cell.HorizontalAlignment = 1;
                table.AddCell(cell);
                table.AddCell("Product");
                table.AddCell("Quantity");
                table.AddCell("Price(BGN)");
                table.AddCell("Location");
                table.AddCell("Sum(BGN)");
                decimal sum = 0;
                foreach (var item in enteries)
                {
                    table.AddCell(item.Product);
                    table.AddCell(item.PQuantity);
                    table.AddCell(item.Price.ToString());
                    table.AddCell(item.Location);
                    table.AddCell(item.PSum.ToString());
                    sum += item.PSum;
                }
                PdfPCell cellEnd = new PdfPCell(new Phrase(Header));
                cellEnd.Colspan = 4;
                cell.HorizontalAlignment = 2;
                table.AddCell(cellEnd);
                table.AddCell(sum.ToString());
            
                doc.Add(table);
                //Paragraph paragraph = new Paragraph();
                //paragraph.Alignment = Element.ALIGN_CENTER;
                //paragraph.Add("text");
                //doc.Add(paragraph);    
                
            }

        }
        static void Main(string[] args)
        {

            IEnumerable<Entery> enteries = new List<Entery> 
            {
                new Entery("re", "ras", 12.7M, "asasfa", 145.4M),
                new Entery("re", "ras", 12.7M, "asasfa", 145.4M),
                new Entery("re", "ras", 12.7M, "asasfa", 145.4M),
                new Entery("re", "ras", 12.7M, "asasfa", 145.4M)
            };
            
            ExtractToPdf("se.pdf", "header", enteries);
            
            //IEnumerable<Vendor> vendors = GetVendors();
            //foreach (var item in vendors)
            //{
            //    Console.WriteLine(item.Name);
            //}
        }
    }
}

